//
//  LXMoreTableViewController.m
//  INMOTION_CustomMadeProject
//
//  Created by cjg on 2021/3/22.
//  Copyright © 2021 cjg. All rights reserved.
//

#import "LXMoreTableViewController.h"
#import "LXMoreSettingCell.h"
#import "IMTConfig.h"
#import "MoreSettingViewController.h"
#import "LXLanguageViewController.h"
#import "KSLanguageManager.h"
#import "IMTWebViewController.h"
#import "LXCustom.h"
#import "NSObject+HUD.h"

@interface LXMoreTableViewController ()

@property (nonatomic , strong)NSMutableArray *lists;

@end

@implementation LXMoreTableViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = IMTNewCarLocalizedString(@"activity_main_tab_more");
    self.tableView.backgroundColor = kIMTDefaultBackgroundColor;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    NSString *contact = [LXCustom sharedCustom].contact;
    //售后
    NSString *seller = [NSString stringWithFormat:@"%@\n\n%@",IMTNewCarLocalizedString(@"fragment_more_customer_care_content"),contact];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    // app build版本
    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    NSString *version = [NSString stringWithFormat:@"%@(%@)",app_Version,app_build];

    // 添加操作指引
    if ([LXCustom sharedCustom].showOperationGuide) {
        [self.lists addObject:@{@"image":@"more_intro",@"title":IMTNewCarLocalizedString(@"operation_guidelines"),@"content":IMTNewCarLocalizedString(@"Scooter url")}];
    }
    // 添加安装视频
    if ([LXCustom sharedCustom].showInstallVideo) {
        [self.lists addObject:@{@"image":@"ic_install",@"title":IMTNewCarLocalizedString(@"Scooter assembly"),@"content":@"http://vorcool.com/video/1-1.mp4"}];
    }
    // 客户关怀
    [self.lists addObject:@{@"image":@"more_service",@"title":IMTNewCarLocalizedString(@"fragment_more_customer_care"),@"content":seller}];
    
    // APP版本
    [self.lists addObject:@{@"image":@"more_version",@"title":IMTNewCarLocalizedString(@"fragment_more_app_version"),@"content":app_Version}];  //version
    
}

- (NSMutableArray *)lists {
    if (!_lists) {
        _lists = [[NSMutableArray alloc] init];
    }
    return _lists;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.lists.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSDictionary *tempDict = self.lists[indexPath.section];
    cell.imageView.image = [UIImage imageNamed:tempDict[@"image"]];
    cell.textLabel.text = tempDict[@"title"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *tempDict = self.lists[indexPath.section];
    
    NSString *title = [tempDict objectForKey:@"title"];
    if ([title isEqualToString:IMTNewCarLocalizedString(@"operation_guidelines")] || [title isEqualToString:IMTNewCarLocalizedString(@"Scooter assembly")]) {
        // 操作指引、安装视频
        IMTWebViewController *web = [[IMTWebViewController alloc] init];
        web.url = tempDict[@"content"];
        [self.navigationController pushViewController:web animated:YES];
    } else {
        // 客户关怀、APP版本
        
        // 直接跳转拨打电话
        if ([LXCustom sharedCustom].contactCall && [tempDict[@"title"] isEqualToString:IMTNewCarLocalizedString(@"fragment_more_customer_care")] && [LXCustom sharedCustom].contact) {
//            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",[LXCustom sharedCustom].contact]]];
            
            NSString *tel = [NSString stringWithFormat:@"tel://%@",[LXCustom sharedCustom].contact];
            
            if (@available(iOS 10.0, *)) {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel] options:@{} completionHandler:^(BOOL success) {
                    if (!success) {
                        MoreSettingViewController *vc = [[MoreSettingViewController alloc] initWithNibName:@"MoreSettingViewController" bundle:nil];
                        vc.title = tempDict[@"title"];
                        vc.content = tempDict[@"content"];
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }];
            } else {
                // Fallback on earlier versions
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
            }
            
            return;
        }
        
        MoreSettingViewController *vc = [[MoreSettingViewController alloc] initWithNibName:@"MoreSettingViewController" bundle:nil];
        vc.title = tempDict[@"title"];
        vc.content = tempDict[@"content"];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
